package com.huanghwh.sync.core.service.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.huanghwh.sync.core.service.ILogService;
import com.huanghwh.sync.message.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: huanghwh
 * @Date: 2021/12/24 11:51
 * @Description:
 */
@Service
@Slf4j
public class LogServiceImpl implements ILogService {

    @Value("${datasource.config.system}")
    private String systemDataSource;

    @Autowired
    private IMessageService messageService;

    /**
     * 保存日志
     *
     * @param entityList
     * @return
     * @Author huanghwh
     * @Date 2021/12/24 13:47
     */
    @Override
    public boolean save(List<Entity> entityList) {
        try {
            DataSource dataSource = DSFactory.get(systemDataSource);
            int[] insert = Db.use(dataSource).insert(entityList);
            log.info("保存插入失败日志：{}", insert.length);
        } catch (SQLException e) {
            String msg = "日志保存异常";
            log.error(msg);
            e.printStackTrace();
            messageService.sendMessageToBark(msg);
        }
        return true;
    }
}
